package com.lijie.ljblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijie.ljblog.model.Common;
import com.lijie.ljblog.model.HistoryToday;
import com.lijie.ljblog.model.Link;
import com.lijie.ljblog.service.*;
import com.lijie.ljblog.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Title: ljblog_springboot
 * @Description: (首页各种零散的请求)
 * @Author:lijie
 * @Version:1.1.0
 * @since 2021/11/25
 */
@RestController
public class ApiController {
    @Resource
    BlogService blogService;
    @Resource
    PersonService personService;
    @Resource
    LiuyanService liuyanService;
    @Resource
    LinkService linkService;
    @Resource
    UtilService utilService;

    /**
     * 博客数量
     * @return
     */
    @RequestMapping("blogNums")
    public Result<Integer> getBlogNums(){
        Result<Integer> result=new Result<>();
        try{
            Integer blogVisibleNums = blogService.getBlogVisibleNums();
            result.setSuccess();
            result.setData(blogVisibleNums);
        }catch (Exception e){
            result.setErrored("获取可见博客数量出错了");
        }
        return result;
    }

    /**
     * 注册人数
     * @return
     */
    @RequestMapping("registerNums")
    public Result<Integer> getRegisterNums(){
        Result<Integer> result=new Result<>();
        try{
            result= personService.getRejisterNums();
        }catch (Exception e){
            result.setErrored("获取注册用户数量出错了");
        }
        return result;
    }

    /**
     * 留言数量
     * @return
     */
    @RequestMapping("liuyanNums")
    public Result<Integer> liuyanNums(){
        Result<Integer> result=new Result<>();
        try{
            result= liuyanService.getLiuyanNums();
        }catch (Exception e){
            result.setErrored("获取留言数量出错了");
        }
        return result;
    }
    /**
     * 友链数量
     * @return
     */
    @RequestMapping("LinkNums")
    public Result<Integer> getLinkNums(){
        Result<Integer> result=new Result<>();
        try{
            QueryWrapper<Link> queryWrapper=new QueryWrapper<>();
            result.setData(linkService.count(queryWrapper));
        }catch (Exception e){
            result.setErrored("获取友链数量出错了");
        }
        return result;
    }

    @RequestMapping("historyToday")
    public Result<HistoryToday> getHistoryToday(){
        //需要完善 思路：查数据库，如果没有在调请求，并且把数据存起来。
        return  utilService.updateHistoday();
    }

    @RequestMapping("getRSAPublicKey")
    public Result<String> getRSAPublicKey(@RequestBody Common redisKey){
        return utilService.getRSAPublicKey(redisKey.getRedisKey());
    }
}
